package Interface.User;

import Mapper.User.keChengMapper;
import Mapper.Admin.zhujiemian;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import pojo.kechenghuanli;

import java.io.FileInputStream;

public class keCheng extends Application {
    Mapper.User.keChengMapper keChengMapper;
    private static final int ITEMS_PER_PAGE = 15;
    private TableView<kechenghuanli> tableView;
    private Pagination pagination;

    private TextField t_id;
    private TextField t_jieshao;
    private TextField t_shetuanming;
    @Override
    public void start(Stage stage) throws Exception {
        FileInputStream input=new FileInputStream("C:\\Users\\86178\\Videos\\Captures\\走路.gif");
        Image image = new Image(input);
        ImageView imageView =new ImageView(image);
        imageView.setFitWidth(488);
        imageView.setFitHeight(465);

        tableView = createTableView();
        pagination = new Pagination();

        Label id=new Label("id:");
        id.setFont(Font.font(12));
        t_id =new TextField();
        t_id.setPrefColumnCount(8);


        Button gengxin =new Button("查询");
        gengxin.setOnAction(e -> gengxin());


        //实例化不然为null
        keChengMapper =new keChengMapper();

        /**
         * 获取数据
         */
        pagination.setPageFactory(pageIndex -> {
            ObservableList<kechenghuanli> data = keChengMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });

        Pane pane = new Pane();
        //pane.getChildren().add(imageView);
        pane.getChildren().addAll(pagination,id,t_id,gengxin);

        id.setLayoutX(0);
        id.setLayoutY(20);
        t_id.setLayoutX(30);
        t_id.setLayoutY(18);


        gengxin.setLayoutX(0);
        gengxin.setLayoutY(100);


        pagination.setLayoutX(160);
        pagination.setLayoutY(16);

        Scene scene =new Scene(pane);
        stage.setScene(scene);
        stage.setTitle("课程");
        stage.setWidth(650);
        stage.setHeight(650);
        stage.setResizable(false);//窗口不可拉伸
        stage.show();
    }

    // 添加一个方法用于刷新数据
    private void refreshData() {
        int currentPageIndex = pagination.getCurrentPageIndex();
        pagination.setPageFactory(pageIndex -> {
            ObservableList<kechenghuanli> data = keChengMapper.fetchDataFromDatabase(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE);
            tableView.setItems(data);
            return new VBox(tableView);
        });
        // 设置回之前的页数
        pagination.setCurrentPageIndex(currentPageIndex);
    }


    private void tianjia() {
        String xingming = t_jieshao.getText();
        String huiyuanqingkuang = t_shetuanming.getText();

        keChengMapper.tianjia(xingming,huiyuanqingkuang);

        refreshData();
    }


    private void gengxin() {
        String id = t_id.getText();
        int currentPageIndex = pagination.getCurrentPageIndex();
        pagination.setPageFactory(pageIndex -> {
            ObservableList<kechenghuanli> data = keChengMapper.fetchDataFrom(pageIndex * ITEMS_PER_PAGE, ITEMS_PER_PAGE,id);
            tableView.setItems(data);
            return new VBox(tableView);
        });
        // 设置回之前的页数
        pagination.setCurrentPageIndex(currentPageIndex);

       // refreshData();
    }

    private TableView<kechenghuanli> createTableView() {
        TableView<kechenghuanli> tableView = new TableView<>();

        TableColumn<kechenghuanli, String> id = new TableColumn<>("id");
        id.setCellValueFactory(cellData -> cellData.getValue().idProperty());
        TableColumn<kechenghuanli, String> name = new TableColumn<>("名字");
        name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        TableColumn<kechenghuanli, String> createtime = new TableColumn<>("创建时间");
        createtime.setCellValueFactory(cellData -> cellData.getValue().createtimeProperty());
        TableColumn<kechenghuanli, String> kechengchixushijian = new TableColumn<>("持续时间");
        kechengchixushijian.setCellValueFactory(cellData -> cellData.getValue().kechengchixushijianProperty());
        TableColumn<kechenghuanli, String> jiaolianProperty = new TableColumn<>("教练");
        jiaolianProperty.setCellValueFactory(cellData -> cellData.getValue().jiaolianProperty());
        TableColumn<kechenghuanli, String> kechengfeiyongProperty = new TableColumn<>("费用");
        kechengfeiyongProperty.setCellValueFactory(cellData -> cellData.getValue().kechengfeiyongProperty());


        tableView.getColumns().addAll(id, name, createtime, kechengchixushijian,jiaolianProperty,kechengfeiyongProperty);

        return tableView;
    }
}